package com.hsl.housaileibot001.handle;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * 自定义单元格写入处理器示例
 */
public class CustomCellWriteHandler implements CellWriteHandler {

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        // 设置单元格样式
        CellStyle cellStyle = context.getWriteSheetHolder().getSheet().getWorkbook().createCellStyle();

        // 设置数字格式
        DataFormat dataFormat = context.getWriteSheetHolder().getSheet().getWorkbook().createDataFormat();
        cellStyle.setDataFormat(dataFormat.getFormat("#,##0.00"));

        // 设置边框
        cellStyle.setBorderBottom(org.apache.poi.ss.usermodel.BorderStyle.THIN);
        cellStyle.setBorderTop(org.apache.poi.ss.usermodel.BorderStyle.THIN);
        cellStyle.setBorderLeft(org.apache.poi.ss.usermodel.BorderStyle.THIN);
        cellStyle.setBorderRight(org.apache.poi.ss.usermodel.BorderStyle.THIN);

        // 设置背景色
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        cellStyle.setFillPattern(org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND);

        cell.setCellStyle(cellStyle);
    }
}